package com.gitee.wsl.net.server.role.admin

import androidx.annotation.Size
import com.gitee.wsl.net.exceptions.Exceptions
import com.gitee.wsl.net.server.getNumberParam
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import io.ktor.server.routing.route
import kotlinx.serialization.Serializable
import kotlin.js.JsExport

/**
 * Request create [AdminEntity]
 */
@Serializable
data class AdminPostRequest(

    val email: String,

    val role: AdminRole,

    val password: String
)

/**
 * Request update [AdminEntity]
 */
@Serializable
data class AdminPutRequest(
    val role: AdminRole,

    //@field:Size(min = 8, max = 12, message = "Size must be between 8 and 12")
    val password: String? = null
)


fun Route.admins(adminsService: AdminsService) {

    //val adminsService: AdminsService by inject()

    route("/admins") {
        get {
            // check role
            call.checkRoleAuth()
            // act
            /*val response = adminsService.transaction {
                getAll().toModels()
            }
            // response
            call.respond(response)*/
        }
        post {
            // check role
            call.checkRoleAdmin()
            // get request
            val request = call.receive<AdminPostRequest>()
            // act
            /*val response = adminsService.transaction {
                insert(
                    role = request.role,
                    email = request.email,
                    password = request.password,
                ).toModel()
            }
            // response
            call.respond(response)*/
        }
        get("/{id}") {
            // check role
            call.checkRoleAdmin()
            // get request
            val id = call.getNumberParam()
            // act
            /*val response = adminsService.transaction {
                findById(id)?.toModel() ?: throw Exceptions.NotFound()
            }
            // response
            call.respond(response)*/
        }
        put("/{id}") {
            // check role
            call.checkRoleAdmin()
            // get request
            val id = call.getNumberParam()
            val request = call.receive<AdminPutRequest>()
            /*// act
            val response = adminsService.transaction {
                findById(id)?.update(
                    role = request.role,
                    password = request.password,
                )?.toModel() ?: throw Exceptions.NotFound()
            }
            // response
            call.respond(response)*/
        }
        delete("/{id}") {
            // check role
            call.checkRoleAdmin()
            // get request
            val id = call.getNumberParam()
            // act
            /*adminsService.transaction {
                findById(id)?.let {
                    if (it.role == AdminRole.ADMIN && countAdmins() <= 1) {
                        // if last admin
                        throw Exceptions.MethodNotAllowed()
                    }
                    it.delete()
                } ?: throw Exceptions.NotFound()
            }*/
            // response
            call.respond(HttpStatusCode.OK)
        }
    }
}
